package com.dunjx.user.controller;

import com.dunjx.user.pojo.User;

import com.dunjx.user.service.UserService;
import com.github.pagehelper.PageInfo;
import entity.*;
import io.swagger.annotations.*;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/****
 * @Author:admin
 * @Description:
 * @Date 2019/6/14 0:18
 *****/
@Api(value = "UserController")
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;


    @ApiOperation(value = "User条件分页查询", notes = "分页条件查询User方法详情", tags = {"UserController"})
    @PreAuthorize("hasAnyRole('user')")
//    @PreAuthorize("hasAnyAuthority('user')")
    @PostMapping(value = "/search")
    public Result<PageInfo> findPage(User user) {
        //调用userService实现分页条件查询User
        PageInfo<User> pageInfo = userService.findPage(user);
        return new Result(true, StatusCode.OK, "查询成功", pageInfo);
    }

    @SneakyThrows
    @ApiOperation(value = "添加/修改用户", notes = "添加/修改用户", tags = {"UserController"})
    @PostMapping(value = "/save")
    public Result<Void> save(User user, @RequestParam(value = "file", required = false) MultipartFile file) {
        boolean isId = true;
        if (StringUtils.isEmpty(user.getId())) {
            isId = true;
        } else {
            isId = false;
        }
        /*参数校验*/
        Result checkResult = ifCheck(user);
        if (!checkResult.isFlag()) {
            return checkResult;
        }
        if (isId) {
            User userinfo = userService.findOneUser(user.getUsername());
            if (userinfo != null) {
                return new Result(false, StatusCode.ERROR, "该用户名已存在。");
            }
        }
        userService.save(user, file);
        if (isId) {
            return new Result(true, StatusCode.OK, "用户添加成功");
        }
        return new Result(true, StatusCode.OK, "用户修改成功");
    }


    @ApiOperation(value = "查询全部", notes = "查询全部用户", tags = {"UserController"})
    @GetMapping("/searchAll")
    public Result<List<User>> findAll() {
        //调用UserService实现查询所有User
        List<User> list = userService.findAll();
        return new Result<List<User>>(true, StatusCode.OK, "查询成功", list);
    }

    /**
     * 参数校验
     *
     * @param user
     * @return
     */
    private Result ifCheck(User user) {
        if (!StringUtils.isEmpty(user.getPassword())) {
            boolean password = ValidatorUtil.isPassword(user.getPassword());
            if (!password) {
                return new Result(false, StatusCode.ERROR, "密码格式不正确！");
            }
        }
        if (!StringUtils.isEmpty(user.getEmail())) {
            boolean email = ValidatorUtil.isEmail(user.getEmail());
            if (!email) {
                return new Result(false, StatusCode.ERROR, "请输入正确的邮箱！");
            }
        }
        if (!StringUtils.isEmpty(user.getPhone())) {
            boolean email = ValidatorUtil.isMobile(user.getPhone());
            if (!email) {
                return new Result(false, StatusCode.ERROR, "请输入正确的手机号！");
            }
        }

        return new Result(true, StatusCode.OK, "参数校验正确");

    }

    /*根据用户名查询该用户信息*/
    @GetMapping( "/findByUsername/{username}")
    public Result<User> findByUsername(@PathVariable("username")String username) {
        User oneUser = userService.findOneUser(username);
        System.out.println("username"+username);
        return new Result(true, StatusCode.OK, "查询成功", oneUser);
    }
}
